﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace CustomAutoCompleteBox
{
	/// <summary>
	/// AutoCompleteBox2.xaml 的交互逻辑
	/// </summary>
	public partial class AutoCompleteBox2 : TextBox
	{
		private ListBox listBox;
		private Popup popupBox;
		public AutoCompleteBox2()
		{
			InitializeComponent();

			popupBox = textBox.Resources["popupBox"] as Popup;
			popupBox.PlacementTarget = textBox;
			popupBox.Placement = PlacementMode.Bottom;
			this.listBox = popupBox.Child as ListBox;

			ResultList = new List<string>();
			SourceList = new List<string>();

		}

		/// <summary>
		/// 要搜索的源列表
		/// </summary>
		public List<string> SourceList { get; set; }

		/// <summary>
		/// 搜索结果列表
		/// </summary>
		public List<string> ResultList { get; set; }

		/// <summary>
		/// 要搜索的字符串
		/// </summary>
		public string SearchText { get; set; }

		/// <summary>
		/// 处理textBox的内容改变事件，实时进行搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void textBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			//MessageBox.Show(textBox.Text);
			//如果textBox没有焦点，不弹listBox
			if (!textBox.IsFocused)
			{
				return;
			}

			SearchText = textBox.Text;
			if (SearchText != string.Empty)
			{
				/// <summary>
				/// 使用匿名委托 delegate(string s) 来处理FindAll
				/// 查找 popupList里面所有包含 SearchText的项
				/// </summary>
				ResultList.Clear();
				ResultList = SourceList.FindAll(delegate (string s)
				{
					return s.ToLower().IndexOf(SearchText.ToLower()) >= 0 ? true : false;
					//return s.ToLower().Contains(SearchText.ToLower());
				});

				if (ResultList.Count > 0)
				{
					listBox.ItemsSource = ResultList;
					listBox.SelectedIndex = 0;
					popupBox.IsOpen = true;

					// 设置popupBox外观				
					popupBox.Width = textBox.ActualWidth;
					popupBox.MaxHeight = 2 * textBox.ActualWidth;
				}
				else
				{
					popupBox.IsOpen = false;
				}

			}
			else
			{
				popupBox.IsOpen = false;
			}
		}

		/// <summary>
		/// 处理textBox按键消息，
		/// 当按的是方向键↑↓时，改变listBox下拉框选中项
		/// 当按的是回车键 enter时，将listBox当前选中项设置为textBox的内容
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void textBox_KeyDown(object sender, KeyEventArgs e)
		{
			//MessageBox.Show(e.Key.ToString());
			//方向键盘确定选中
			if (e.Key == Key.Down)
			{
				if (popupBox.IsOpen)
				{
					if (listBox.Items.Count > 0)
					{
						if (listBox.SelectedIndex == -1)
						{
							listBox.SelectedIndex = 0;
						}
						else if (listBox.SelectedIndex == listBox.Items.Count - 1)
						{
							listBox.SelectedIndex = 0;
						}
						else
						{
							listBox.SelectedIndex += 1;
							listBox.ScrollIntoView(listBox.SelectedItem);
						}
					}
				}
			}
			else if (e.Key == Key.Up)
			{
				if (popupBox.IsOpen)
				{
					if (listBox.Items.Count > 0)
					{
						if (listBox.SelectedIndex == -1)
						{
							listBox.SelectedIndex = 0;
						}
						else if (listBox.SelectedIndex == 0)
						{
							listBox.SelectedIndex = listBox.Items.Count - 1;
						}
						else
						{
							listBox.SelectedIndex -= 1;
							listBox.ScrollIntoView(listBox.SelectedItem);
						}
					}
				}
			}
			else if (e.Key == Key.Enter)
			{
				textBox.SetCurrentValue(TextBox.TextProperty, listBox.SelectedItem.ToString());
				textBox.CaretIndex = textBox.Text.Length;
				popupBox.IsOpen = false;
			}
		}

		/// <summary>
		/// 处理listBox的按键消息
		/// 当按的是回车键 enter时，把listBox的当前选中项设置到textBox中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listBox_PreviewKeyUp(object sender, KeyEventArgs e)
		{

			if (e.Key == Key.Enter)
			{
				textBox.SetCurrentValue(TextBox.TextProperty, listBox.SelectedItem.ToString());
				popupBox.IsOpen = false;
			}
		}

		/// <summary>
		/// 处理listBox的按键消息
		/// 当按的是回车键 enter时，把listBox的当前选中项设置到textBox中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			textBox.SetCurrentValue(TextBox.TextProperty, listBox.SelectedItem.ToString());
			popupBox.IsOpen = false;
		}

	}
}
